home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / blx11.zip / WINAPP.H < prev    next >
Text File  |  1991-01-21  |  8KB  |  211 lines

  1. #if !defined(WINAPP_H)
  2. /* WINAPP:  An object of this class is defined once for every application. It
  3.     contains the items passed to WinMain, as well as access functions for
  4.     returning them. The constructor initializes them. */
  5. class WinApp { // Windows application class
  6.     static HANDLE hInstance;
  7.     static HANDLE hPrevInstance;
  8.     static LPSTR lpszCmdLine;
  9.     static int nCmdShow;
  10. public:
  11.     WinApp(HANDLE hinst, HANDLE hpinst, LPSTR cmdline, int cmdshow) {
  12.         hInstance = hinst;
  13.         hPrevInstance = hpinst;
  14.         lpszCmdLine = cmdline;
  15.         nCmdShow = cmdshow;
  16.     }
  17.     static HANDLE GetInstance(void) { return hInstance; }
  18.     static HANDLE GetPrevInstance(void) { return hPrevInstance; }
  19.     static LPSTR GetCmdLine(void) { return lpszCmdLine; }
  20.     static int GetCmdShow(void) { return nCmdShow; }
  21.     static int MessageLoop(void) { // default message loop processing
  22.         MSG msg;
  23.         while(GetMessage(&msg, NULL, 0, 0)) {
  24.             TranslateMessage(&msg);
  25.             DispatchMessage(&msg);
  26.         }
  27.         return msg.wParam;
  28.     }
  29. };
  30. /* WINCLASS */
  31. const NOSTYLE = 0;
  32. const NOEXTRABYTES = 0;
  33. class WinClass : WNDCLASS {
  34.     BOOL class_registered;
  35. public:
  36.     WinClass(void) {
  37.         style = NOSTYLE;
  38.         lpfnWndProc = DefWindowProc;
  39.         cbClsExtra = NOEXTRABYTES;
  40.         cbWndExtra = NOEXTRABYTES;
  41.         hInstance = NULL;
  42.         hIcon = LoadIcon(NULL, IDI_APPLICATION);
  43.         hCursor = LoadCursor(NULL, IDC_ARROW);
  44.         hbrBackground = GetStockObject(WHITE_BRUSH);
  45.         lpszMenuName = NULL;
  46.         lpszClassName = NULL;
  47.         class_registered = FALSE;
  48.     }
  49.     // since 'this' is derived from WNDCLASS, it's passed as one
  50.     void Register(void) {
  51.         RegisterClass(this);
  52.         class_registered = TRUE;
  53.     }
  54.     BOOL Registered(void)  { return class_registered; }
  55.     friend class Window;     // allow Window to modify members
  56. };
  57.  
  58. /* WINHANDLE:  An object of this class is created with every instance
  59.     of Window (below). It contains data that controls Window creation
  60.     handling, as well as the window handle. */
  61. class WinHandle {
  62.     HWND hWnd;
  63.     LPSTR classname;
  64.     LPSTR windowname;
  65.     DWORD winstyle;
  66.     int upper_left_x;
  67.     int upper_left_y;
  68.     int winwidth;
  69.     int winheight;
  70.     HWND winParent;
  71.     HMENU menu;
  72.     HANDLE hInstance;
  73.     LPSTR lpParam;
  74. public:
  75.     WinHandle(void) {
  76.         hWnd = NULL;
  77.         classname = (LPSTR)"WinApp";
  78.         windowname = (LPSTR)"WinApp:Window";
  79.         winstyle = 0;
  80.         upper_left_x = upper_left_y = winwidth = winheight = 0;
  81.         winParent = NULL;
  82.         menu = NULL;
  83.         hInstance = 0;
  84.         lpParam = NULL;
  85.     }
  86.     HWND GetHandle(void) { return hWnd; }
  87.     HWND Create(void) {
  88.         if(hWnd)           // if window's already created, return TRUE
  89.             return TRUE;
  90.         hWnd = CreateWindow(classname,windowname,winstyle,
  91.                           upper_left_x,upper_left_y,winwidth,winheight,
  92.                           winParent,menu,hInstance, lpParam);
  93.         return (hWnd ? TRUE : FALSE);
  94.     }
  95.     friend class Window;          // allow Window to modify members
  96. };
  97. /* WINDOW:  Every window is derived from this abstract class.
  98.     Every class derived from this one must have a WinClass object
  99.     (or a pointer to one created by a derived class) and pass it back
  100.     to Window. And each must supply its own WndProc for the class. */
  101. class Window {
  102.     WinHandle WHdl;
  103.     int current_display;
  104.     int previously_visible;
  105.     void Show(void) {
  106.         previously_visible = ShowWindow(GetHandle(),current_display);
  107.     }
  108. protected:
  109.     WinClass *MyWc;
  110.     WinApp *MyApp;
  111. public:
  112.     Window(WinApp *app, WinClass *wc) {
  113.         MyApp = app;
  114.         MyWc = wc;
  115.         DefaultDisplay();
  116.         previously_visible = FALSE;
  117.     };
  118.     // displays window and creates if not already created
  119.     BOOL Display(void) {
  120.         Register();
  121.         if(!Create()) return FALSE;
  122.         Show();
  123.         Update();
  124.         return TRUE;
  125.     }
  126.     BOOL Display(int display_style) {
  127.         current_display = display_style;
  128.         return Display();
  129.     }
  130.     void Register(void) {
  131.         if(!MyWc->Registered()) {            // if class not registered
  132.             SetClassWinXbytes(sizeof(Window *));
  133.             SetClassInstance();         // insert instance handle
  134.             if(!GetClassName())        // if class name not set
  135.                 SetClassName("WinApp");
  136.             MyWc->Register();         // register the class
  137.         }
  138.     }
  139.     BOOL Create(void)  {
  140.         Register();
  141.         WHdl.hInstance = MyApp->GetInstance();
  142.         WHdl.classname = MyWc->lpszClassName;
  143.         WHdl.lpParam = (LPSTR)this;
  144.         return WHdl.Create();
  145.     }
  146.     HWND GetHandle(void) { return WHdl.hWnd; }
  147.     void Hide(void) // hides the window
  148.         { Display(SW_HIDE); }
  149.     void Minimize(void) // minimizes the window
  150.         { Display(SW_MINIMIZE); }
  151.     void Maximize(void) // maximizes the window
  152.         { Display(SW_SHOWMAXIMIZED); }
  153.     void Normalize(void) // displays window in original size and position
  154.         { Display(SW_SHOWNORMAL); }
  155.     void DefaultDisplay(void)
  156.         { current_display = MyApp->GetCmdShow(); }
  157.     void Paint(void) {            // paints window
  158.         PAINTSTRUCT ps;
  159.         RECT         rect;
  160.         BeginPaint(GetHandle(), &ps);
  161.         EndPaint(GetHandle(), &ps);
  162.     }
  163.     void Update(void) // updates window
  164.         { UpdateWindow(GetHandle()); }
  165.     // vocab of functions for modifying the registration info
  166.     void SetClassInstance(void)
  167.         { MyWc->hInstance = MyApp->GetInstance(); }
  168.     void SetClassName(LPSTR classname)
  169.         { MyWc->lpszClassName = classname; }
  170.     void SetClassStyle(unsigned newstyle) { MyWc->style = newstyle; }
  171.     void AddClassStyle(unsigned addstyle) { MyWc->style |= addstyle; }
  172.     void SetClassWinProc(long (FAR PASCAL *lpfnWndProc)(HWND, unsigned,
  173.                     WORD, LONG))
  174.         { MyWc->lpfnWndProc = lpfnWndProc; }
  175.     void SetClassWinXbytes(int xtrabytes)
  176.         { MyWc->cbWndExtra = xtrabytes; }
  177.     void SetClassIcon(LPSTR iconname) {
  178.         if(MyWc->hInstance)
  179.             MyWc->hIcon = LoadIcon(MyWc->hInstance,iconname);
  180.     }
  181.     LPSTR GetClassName(void)           { return MyWc->lpszClassName; }
  182.     // vocab of functions for modifying the create info
  183.     void SetWinName(LPSTR winname) { WHdl.windowname = winname; }
  184.     void SetWinStyle(DWORD dword)     { WHdl.winstyle = dword; }
  185.     void AddWinStyle(DWORD dword)    { WHdl.winstyle |= dword; }
  186.     void SetWinX(int x)                            { WHdl.upper_left_x = x; }
  187.     void SetWinY(int y)                            { WHdl.upper_left_y = y; }
  188.     void SetWinWidth(int width)              { WHdl.winwidth = width; }
  189.     void SetWinHeight(int height)           { WHdl.winheight = height; }
  190.     void SetWinInstance(HANDLE hinst) { WHdl.hInstance = hinst; }
  191. };
  192. inline void *GetPointer(HWND hWnd) {
  193.     #if defined(__SMALL__) || defined(__MEDIUM__)
  194.         return (void *)GetWindowWord(hWnd,0);
  195.     #elif defined(__LARGE__) || defined(__COMPACT__)
  196.         return (void *)GetWindowLong(hWnd,0); #else
  197.     #error Must use Small, Medium, Large or Compact models!
  198.     #endif
  199. }
  200. inline void SetPointer(HWND hWnd, void *ptr) {
  201.     #if defined(__SMALL__) || defined(__MEDIUM__)
  202.         SetWindowWord(hWnd,0,(WORD)ptr);
  203.     #elif defined(__LARGE__) || defined(__COMPACT__)
  204.         SetWindowLong(hWnd,0,(LONG)ptr);
  205.     #else
  206.     #error Must use Small, Medium, Large or Compact models!
  207.     #endif
  208. }
  209. #define WINAPP_H
  210. #endif
  211.